// pages/search/search.js
import showDetail from "../../modules/showDetail";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */

  data: {
    goods_list: [],
    search_list: [],
    is_hidden: true,
    pageNum: 1,
    keyword: '',
    totalPages: 5,
    inputword: '',
    keywords: [],
    Authorization: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var Authorization = wx.getStorageSync('Authorization');
    if (Authorization == '') {
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
      wx.reLaunch({
        url: '/pages/openid/openid',
      })
    }
    that.setData({
      Authorization: Authorization,
    })
    wx.getStorageSync('keywords')
  },
  searchInput(e) {
    // console.log(e.detail.value);
    var that = this;
    var Authorization = wx.getStorageSync('Authorization');
    if (Authorization == '') {
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
      wx.reLaunch({
        url: '/pages/openid/openid',
      })
    }
    that.setData({
      Authorization: Authorization,
      inputword: e.detail.value
    })
    // console.info(e.detail.value)
    if (e.detail.value == "") {
      that.setData({
        search_list: [],
      });
      this.data.is_hidden = true;
    } else {
      wx.request({
        url: "http://localhost:8080/wechat/selectgoods",
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          'Authorization': that.data.Authorization
        },
        data: {
          'pageNum': '1',
          'keyword': this.data.keyword
        },
        success: function (res) {
          if (res.statusCode == 200) {
            that.setData({
              totalPages: res.data['totalPages'],
              search_list: res.data['goods']
            });
          } else {
            wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
            wx.reLaunch({
              url: '/pages/openid/openid',
            })
          }

        } // return app.globalData.phone.goods_list.filter(item=>{
        //   return item.header.toString().toLowerCase().indexOf(attr)>-1;
      })
      this.data.is_hidden = false;
    }
    this.setData({
      // search_list,
      keyword: e.detail.value,
      is_hidden: this.data.is_hidden
    });
  },
  search(e) {
    var that = this;
    //按关键字筛选商品，如果关键字匹配到商品名称，则返回该商品列表
    wx.request({
      url: "http://localhost:8080/wechat/selectgoods",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'pageNum': 1,
        'keyword': this.data.keyword
      },
      success: function (res) {
        if (res.statusCode == 200) {
          that.setData({
            totalPages: res.data['totalPages'],
            pageNum: that.data.pageNum + 1,
            goods_list: res.data['goods']
          })
          console.info(res);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }

      }
    })
    // const keywords=wx.getStorageSync('keywords');
    wx.showLoading({
      title: '请稍等',
    });
    setTimeout(() => {
      this.setData({
        // goods_list:this.getList(keywords),
        is_hidden: true
      });
      wx.hideLoading();
    }, 500);
  },
  showDetail,
  showItemDetail(e) {

    //按关键字筛选商品，如果关键字匹配到商品名称，则返回该商品列表
    const header = e.currentTarget.dataset.header.toLowerCase();
    console.log(header);
    wx.showLoading({
      title: '请稍等',
    })
    setTimeout(() => {
      wx.hideLoading()
      this.setData({
        pageNum: 1,
        totalPages: 1,
        goods_list: this.getList(header),
        is_hidden: true
      });
    }, 500)
  },
  stopTouchMove: function () {
    return false;
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  getList(attr) {
    var that = this;
    console.info(wx.getStorageSync('keywords'))
    this.setData({
      keywords: that.data.keywords.concat(that.data.keyword),
      keyword: attr
    });
    return this.data.search_list.filter(item => {
      return item.description.toString().toLowerCase().indexOf(attr) > -1;
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('--------下拉刷新-------')
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.request({
      url: "http://localhost:8080/wechat/selectgoods",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'pageNum': this.data.pageNum + 1,
        'keyword': this.data.keyword
      },
      success: (res) => {
        if (res.statusCode == 200) {
          that.setData({
            pageNum: this.data.pageNum + 1,
            totalPages: res.data['totalPages'],
            goods_list: this.data.goods_list.concat(res.data['goods']),
          })
          wx.hideLoading();
          console.info(this.data.pageNum);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
      complete: () => {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("向上滑动")
    //判断是否还有下一条数据
    if (this.data.pageNum >= this.data.totalPages) {
      console.log(this.data.totalPages);
      console.log(this.data.pageNum);
      //表示没有下一页数据
      //console.log("没有下一页数据")
      wx.showToast({
        title: '已经到底了'
      })
    } else {
      console.info(this.data.pageNum);
      console.info(this.data.totalPages);
      var Authorization = wx.getStorageSync('Authorization')
      if (Authorization == '') {
        wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
        wx.reLaunch({
          url: '/pages/openid/openid',
        })
      } else {
        this.onPullDownRefresh();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})